{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 旋转数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import *\n",
    "class Solution:\n",
    "    def rotate(self, nums: List[int], k: int) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        for i in range(k % 7):\n",
    "            tmp = nums[0]\n",
    "            for i in range(len(nums) -1):\n",
    "                nums[i] = nums[i+1]\n",
    "            nums[-1] = tmp\n",
    "            print(nums)\n",
    "        # nums[1]=100\n",
    "#         print(nums)\n",
    "# i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5, 6, 7, 1]\n",
      "[3, 4, 5, 6, 7, 1, 2]\n",
      "[4, 5, 6, 7, 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "Solution().rotate([1,2,3,4,5,6,7],3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1,2,3,4,5,6,7\n",
    "# 7123456\n",
    "# 6712345\n",
    "\n",
    "tmp = nums[i]\n",
    "nums[i] = nums[i+1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "吐血写了半天写了个左移的算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def rotate(self, nums: List[int], k: int) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        length = len(nums)\n",
    "        for i in range(k % length):\n",
    "            tmp = nums[length-1]\n",
    "            for i in range(length-1, 0, -1):\n",
    "                nums[i] = nums[i-1]\n",
    "            nums[0] = tmp\n",
    "            print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 1, 2, 3, 4, 5, 6]\n",
      "[6, 7, 1, 2, 3, 4, 5]\n",
      "[5, 6, 7, 1, 2, 3, 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Solution().rotate([1,2,3,4,5,6,7],3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
